using System;
using Microsoft.SPOT;

namespace NetController.MMA7455L
{
    public class Offsets
    {
        #region Constructor
        public Offsets(GLVL GLVL)
        {
            this.X = 0;
            this.Y = 0;
            this.Z = 0;
            this.GLVL = GLVL;
        }
        #endregion

        #region Methods

        #region CalculateOffset
        public void CalculateOffset(Axis axis, int value)
        {
            if (value != 0)
            {
                switch (axis)
                {
                    case Axis.X:
                    case Axis.Y:
                        value = value * -1;

                        switch (this.GLVL)
                        {
                            case GLVL.Graus8:
                                value = value * 8;
                                break;
                            case GLVL.Graus2:
                                value = value * 2;
                                break;
                            case GLVL.Graus4:
                                value = value * 4;
                                break;
                        }

                        if (axis == Axis.X) this.X += value;
                        else this.Y += value;
                        break;
                    case Axis.Z:
                        int ideal = 0;
                        switch (this.GLVL)
                        {
                            case GLVL.Graus8:
                                ideal = 64 * 8;
                                break;
                            case GLVL.Graus2:
                                ideal = 64 * 2;
                                break;
                            case GLVL.Graus4:
                                ideal = 64 * 4;
                                break;
                        }

                        value = ideal - value;
                        switch (this.GLVL)
                        {
                            case GLVL.Graus8:
                                value = value * 8;
                                break;
                            case GLVL.Graus2:
                                value = value * 2;
                                break;
                            case GLVL.Graus4:
                                value = value * 4;
                                break;
                        }

                        this.Z += value;
                        break;
                }
            }
        }
        #endregion

        #region SendOffset
        public void SendOffset(AcelMMA7455L device, int value, byte lsbAddress, byte msbAddress)
        {
            var lsb = (byte)value;
            var msb = (byte)(value >> 8);

            device.WriteByte(lsbAddress, new byte[] { lsb }, device.TimeOut);
            device.WriteByte(msbAddress, new byte[] { msb }, device.TimeOut);
        }
        #endregion

        #endregion

        #region Properties
        public int X { get; set; }
        public int Y { get; set; }
        public int Z { get; set; }
        public GLVL GLVL { get; private set; }
        #endregion
    }
}
